//
//  KLMacro.h
//  KoalaUtilKit
//
//  Created by Tommy on 15/1/14.
//  Copyright (c) 2015年 Koala Team. All rights reserved.
//

#ifndef KoalaUtilKit_KLMacro_h
#define KoalaUtilKit_KLMacro_h


#define STRINGIFY(S) #S
#define DEFER_STRINGIFY(S) STRINGIFY(S)
#define PRAGMA_MESSAGE(MSG) _Pragma(STRINGIFY(message(MSG)))
#define FORMATTED_MESSAGE(MSG) "[TODO-" DEFER_STRINGIFY(__COUNTER__) "] " MSG " \n" \
DEFER_STRINGIFY(__FILE__) " line " DEFER_STRINGIFY(__LINE__)
#define KEYWORDIFY try {} @catch (...) {}
// 最终使用下面的宏
#define TODO(MSG) KEYWORDIFY PRAGMA_MESSAGE(FORMATTED_MESSAGE(MSG))




//error
#ifndef KLError
#define KLError(__domain,__code,__userInfo) \
        [NSError errorWithDomain:__domain code:__code userInfo:__userInfo]]

#define KLSimpleError(__domain,__code,__desc) \
        [NSError errorWithDomain:__domain code:__code userInfo:@{NSLocalizedDescriptionKey: __desc?:@"未知错误"}]

#define KLSendError(__domain,__code,__userInfo) \
        [subscriber sendError:KLError(__domain,__code,__userInfo)]

#define KLSendSimpleError(__domain,__code,__desc) \
        [subscriber sendError:KLSimpleError(__domain,__code,__desc)]
#endif


//alert
#define KLShowAlert(title) UIAlertView *alertView =     \
                            [[UIAlertView alloc]initWithTitle:nil \
                                                message:title \
                                                delegate:nil \
                                                cancelButtonTitle:nil \
                                                otherButtonTitles:@"确定", nil];   \
                            [alertView show];

#define KLShowAlertWithCancel(title) UIAlertView *alertView =     \
                              [[UIAlertView alloc]initWithTitle:nil \
                                    message:title \
                                    delegate:nil \
                                    cancelButtonTitle:@"取消" \
                                    otherButtonTitles:@"确定", nil];   \
                               [alertView show];

#define KLShowAlertMore(title,cancel,...) UIAlertView *alertView =     \
                                    [[UIAlertView alloc]initWithTitle:nil \
                                            message:title \
                                            delegate:nil \
                                            cancelButtonTitle:cancel \
                                            otherButtonTitles:__VA_ARGS__];   \
                                    [alertView show];




//rac
#define KLRACSend(value)    {  \
                              [subscriber sendNext:value];\
                              [subscriber sendCompleted];\
                             }

#define KLRACCmd(_block_)       [[RACCommand alloc]initWithSignalBlock:_block_]
#define KLRACSignal(_block_)    [RACSignal createSignal:_block_]




//UI
#define HideKeyBoard() [[[UIApplication sharedApplication] keyWindow] endEditing:YES]

#define HideKeyBoardForView(_view_) {   \
                                        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc]init]; \
                                        [[gesture rac_gestureSignal]subscribeNext:^(id x) { \
                                            HideKeyBoard();  \
                                        }];\
                                        [_view_ addGestureRecognizer:gesture];\
                                    }














#endif
